package com.qdxwx.manage.controller.teacher;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.manager.ManagerService;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.subject.SubjectService;
import com.qdxwx.data.service.teacher.TeacherApplyService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.data.service.user.UserService;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.category.Category;
import com.qdxwx.models.subject.Subject;
import com.qdxwx.models.teacher.Teacher;
import com.qdxwx.models.teacher.TeacherApply;
import com.qdxwx.models.user.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 讲师Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/teacher/apply")
public class TeacherApplyController extends BaseController {

    @Autowired
    private TeacherApplyService teacherApplyService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private ManagerService managerService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private UserService userService;

    @Permissions("teacher:apply")
    @GetMapping("list.html")
    public String list() {
        return "teacher/teacher-apply";
    }

    @PostMapping("/list.json")
    @Permissions("teacher:apply")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        String searchKey = request.getParameter("searchKey");
        String status = request.getParameter("status");
        TeacherApply teacherApply = new TeacherApply();
        if(TKString.isNotEmpty(searchKey)){
            if(TKString.isMobile(searchKey)){
                teacherApply.setMobile(searchKey);
            }else {
                teacherApply.setName(searchKey);
            }
        }
        teacherApply.setStatus(TKString.isNotEmpty(status) ? TKConvert.toInteger(status) : null);
        List<TeacherApply> list = teacherApplyService.getList(teacherApply, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(teacherApplyService.count(teacherApply));
        return rspData;
    }

    @GetMapping("/info.html")
    public String info(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if(id < 1) return error404;
        TeacherApply teacherApply = teacherApplyService.get(id);
        if(teacherApply == null) return error404;
        Category category = categoryService.get(teacherApply.getSubjectId());
        mmap.put("teacherApply",teacherApply);
        mmap.put("subject", category);
        return "teacher/teacher-apply-info";
    }

    @PostMapping("/examine.json")
    @Permissions("teacher:apply")
    @ResponseBody
    public AjaxResult examine(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if(id < 1) return error("缺少id参数");
        TeacherApply apply = teacherApplyService.get(id);
        if(apply == null) return error("找不到该申请记录");
        if(apply.getStatus().equals(1)) return error("已通过申请，无需再次审核");
        int status = TKConvert.toInteger(request.getParameter("status"));
        if(status < 1 || status > 2) error("参数异常");
        if (teacherService.checkMobile(apply.getMobile())) return error("该手机号已被注册");
        apply.setStatus(status);
        int row = teacherApplyService.update(apply);
        if(row > 0 && status == 1){
            Subject subject = subjectService.get(apply.getSubjectId());
            Teacher teacher = new Teacher();
            teacher.setName(apply.getName());
            teacher.setMobile(apply.getMobile());
            teacher.setEducation(apply.getEducation());
            teacher.setIntro(apply.getIntro());
            teacher.setAvatar(apply.getAvatar());
            teacher.setIsDisabled(0);
            if(subject != null){
                teacher.setSubjectId(subject.getSubjectId());
            }
            User user = userService.getByMobile(teacher.getMobile());
            if (user != null) {
                teacher.setUserId(user.getUserId());
            }
            int count = teacherService.insert(teacher);
            if(count > 0){
                //没有前台用户创建前台用户
                if(teacher.getUserId() < 1){
                   teacher.setUserId(userService.insertByTeacher(teacher));
                }
                //同时创建管理员
                if(TKConvert.toInteger(request.getParameter("createManager")) == 1){
                    teacher.setManagerId(managerService.insertByTeacher(teacher));
                }
                teacherService.update(teacher);
            }

        }
        return toAjax(row);
    }

    /**
     * 删除
     */
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("ids"));
        TeacherApply teacherApply = teacherApplyService.get(id);
        if(teacherApply == null) return error("找不到该入驻信息");
        if (teacherApply.getStatus().equals(1)) return error("通过入驻信息不能删除");
        return toAjax(teacherApplyService.delete(id));
    }

}
